#coding=utf-8
'''
Created on Dec 10, 2015
@author: DQA wangpeng 
'''
import data.desired_setting

import data.logging_save

import time
from appium import webdriver
import data.config
import data.get_status
import threading
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
import sys
from time import ctime
reload(sys)
sys.setdefaultencoding('utf-8')

import data.logging_save
import data.Create_Log_Folder
import unittest

class guess(unittest.TestCase):
    def setUp(self):
        #startup the huawei m130 app
        self.save_log = data.Create_Log_Folder.Create('xiami_swipe_show')
        desired_caps = data.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        data.logging_save.logging.info("startup the app already")
        print 'startup the app already'
        
    def tearDown(self):
        self.driver.close_app()
        
        
    def test_alert(self):
        u"""测试步骤：点击“虾米”音乐首页的音乐“轮播”，然后点击进去，播放第一首歌曲，再按“后退”返回按键，循环播放10次       测试依据：播放list的歌曲名和底部播放器底部的text名字"""

        #excute the test case times
        playback_times = 1
        success_times = 1
        fail_times = 1
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        print save_pic
        
        while(playback_times<=10):
            alert_number = 1
            while(alert_number <=10):
                try:
                    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                    time.sleep(3)
                    alert = self.driver.switch_to_alert()
                    alert.accept()
                    alert_number = alert_number +1
                    
                    if alert_number == 10:
                        data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                        print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                        self.driver.get_screenshot_as_file(save_pic)
                        savenumber = savenumber + 1
                        self.fail("the network envoiroment is too bad")
                        
                except:
                    time.sleep(10)
                    self.driver.find_element_by_xpath(data.config.Get_xiami_swipe_show()).click()
                    data.logging_save.logging.info("点击swipe图片轮播链接，进入到播放界面")
                    print time.ctime(),"点击swipe图片轮播链接，进入到播放界面"
                    alert_number = 11
                
            
            while(alert_number <=15):        
                try:
                    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                    time.sleep(3)
                    alert = self.driver.switch_to_alert()
                    alert.accept()
                    alert_number = alert_number +1
                    print alert_number
                    print '1234444444444444444444'
                    if alert_number == 10:
                        data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                        print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                        self.driver.get_screenshot_as_file(save_pic)
                        savenumber = savenumber + 1
                        self.fail("the network envoiroment is too bad")
                        
                except:
                    print '44444'
                    time.sleep(10)
                    #WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.Xiami_first_song_name())))    

                    
                    self.driver.find_element_by_xpath(data.config.Xiami_first_song_name()).click()
                    
                    time.sleep(10)
                    
                    playback_list_name = self.driver.find_element_by_xpath(data.config.Xiami_first_song_name()).text

                    
                    data.logging_save.logging.info("播放列表第一首歌曲名字是："+str(playback_list_name))
                    #for htmltestrunner display
                    print time.ctime(),"播放列表第一首歌曲名字是：%s"%playback_list_name
                    time.sleep(5)
                    
                    get_songname_http = data.get_status.songname()
                    time.sleep(5)
                    data.logging_save.logging.info("通过网络获取到的歌曲名字是:"+str(get_songname_http))
                    print time.ctime(),"通过网络获取到的歌曲名字是：%s"%get_songname_http
              
                    time.sleep(2)
    
                    try:
                        self.assertIn(playback_list_name, get_songname_http) 
                        print ctime(),"经过对比，文件名比对一致：成功次数为 %d 次"%success_times
                        data.logging_save.logging.info("经过对比，文件名比对一致：成功次数为"+str(success_times)+"次")
                        success_times = success_times + 1
                        time.sleep(4)
                        self.driver.find_element_by_xpath(data.config.back_previous_menu()).click()
                        print 'back to the menu'
                        data.logging_save.logging.info("back to the menu")
                        time.sleep(5)
                        alert_number = 16
                        playback_times = playback_times + 1
                        
                    except:
                        data.logging_save.logging.warning("底部获取名字和实际获取名字不一致")
                        print time.ctime(),"底部获取名字和实际获取名字不一致"
                        self.driver.get_screenshot_as_file(save_pic)
                        savenumber = savenumber + 1
                        self.fail("the current song name do not match the real songname after playback")
             
if __name__ == '__main__':
    unittest.main()
    print 'finished'